package com.lz.thymeleaf.controller;

import com.lz.thymeleaf.pojo.Admin;
import com.lz.thymeleaf.service.AdminService;
import com.lz.thymeleaf.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: cp9
 * @Date: 2021/01/28/16:07
 * @Description:
 */
@Slf4j
@Controller
public class MenuController {
    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;
    //登录拦截界面
    @RequestMapping({"/index","/"})
    public String index(){
        log.info("------请登录------");
        return "views/login";
    }
    //管理员界面
    @RequestMapping("/admin")
    public String admin(Model model){
        //获取登录用户信息
        //直接从SecurityContextHolder中拿到Authentication对象
        String adminName = SecurityContextHolder.getContext().getAuthentication().getName();
        model.addAttribute("adminName",adminName);
        return "views/dashboard/admin";
    }
    //主页控制台页面
    @RequestMapping("/home")
    public String home(ModelMap map){
        int count = userService.findUserCount();
        int addressNum = userService.findUserAddressNum();
        int adminNum = adminService.findAdminNum();
        map.addAttribute("userNum",count);
        map.addAttribute("userAddressNum",addressNum);
        map.addAttribute("adminNum",adminNum);
        return "views/dashboard/first_page";
    }
    //用户列表页面
    @RequestMapping("/userlist")
    public String user(){
        return "views/user/userlist";
    }
    //用户可视化页面
    @RequestMapping("/uservisual")
    public String uservisual(ModelMap map){
        List<Integer> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<Map> list = userService.queryForVisual();
        for (Map m : list){
            Map mp ;
            mp = m;
            list1.add(Integer.valueOf(mp.get("num").toString()));
            list2.add(mp.get("address").toString());
        }
       map.addAttribute("num",list1);
       map.addAttribute("address",list2);
       return "views/user/user_visual";
    }
    //修改密码
    @RequestMapping("/modpwd")
    public String modpwd(){
        return "views/modpassword";
    }
    //关于页面
    @RequestMapping("/about")
    public String about() { return "views/about"; }
    //退出登录
    @RequestMapping("/logout")
    public String logOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        Admin sessionAdmin = (Admin) session.getAttribute("admin");
        session.invalidate();
        log.info(sessionAdmin.getName()+"---退出登录----");
        return "views/login";
    }

}
